﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
using System.Drawing;

namespace Voize
{
    internal partial class fSearch : Form
    {


        internal fSearch(List<string> MusicCollection,CPlayer player,DataGridView dPlaylist)
        {


            InitializeComponent();
            m_Player = player;
            m_dPlaylist = dPlaylist;
            m_MusicCollection = MusicCollection;
            CTagger tagger = new CTagger();
            string artist = null;
            string title = null;

            foreach (string song in MusicCollection)
            {
				ListViewItem item = new ListViewItem();
				if (song.StartsWith("http://"))
					item.Text = song;
				else
					item.Text = Path.GetFileNameWithoutExtension(song);
				
				item.Name = song;

				if (!song.StartsWith("http://"))
				{
					artist = tagger.GetFirstArtist(song);
					title = tagger.GetSong(song);

					if (artist != null)
					{
						item.Text = artist.Replace("-", "") + " - " + title;
					}
				}
                this.lSongs.Items.Add(item);
            }               
        }



        private string m_Song;
        private CPlayer m_Player;
        private DataGridView m_dPlaylist;
        private List<string> m_MusicCollection;



        internal String Song
        {
            get
            {
                if (m_Song != null)
                    return m_Song;
                else
                    return "Nothing";
            }
        }



        private void tSearch_TextChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < this.lSongs.Items.Count; i++)
            {
                this.lSongs.Items[i].Selected = false;

                if (tSearch.Text == "")
                {
                    lSongs.Items[i].BackColor = Color.White;
                    lSongs.Items[i].ForeColor = Color.Black;
                }
            }

            if (this.tSearch.Text != "")
            {
                for (int j = 0; j < this.lSongs.Items.Count; j++)
                {
                    if (this.lSongs.Items[j].Text.Trim().ToLower().Contains
                                                (this.tSearch.Text.Trim().ToLower()))
                    {
                        this.lSongs.Items[j].Selected = true;
                        this.lSongs.Items[j].EnsureVisible();
                    }
                }
            }
        }



        private void listView1_ItemActivate(object sender, EventArgs e)
        {
            m_Song = this.lSongs.SelectedItems[0].Name;
                 
            int row = 0;
            foreach(string item in m_MusicCollection)
            {                
                row += 1;
                if(item.Equals(m_Song))
                {
                    CPlayerStatus.PlayListLoaded = false;
                    CPlayerStatus.WantChangeSong = true;
                    m_dPlaylist[0,row - 1].Selected = true;
                    CPlayerStatus.WantChangeSong = false;
                }
            }
        }



        private void bClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
